package com.ziyun.erp.modules.production.operate;

import com.ziyun.erp.common.e.order.OrderItemStatusEnum;
import com.ziyun.erp.common.e.order.ProductionStatusEnum;
import com.ziyun.erp.common.e.order.ReceivedStatusEnum;
import com.ziyun.erp.common.e.production.AllotTypeEnum;
import com.ziyun.erp.common.e.production.OrderProductionStatusEnum;
import com.ziyun.erp.modules.order.service.ErpOrderLogService;
import com.ziyun.erp.modules.order.service.ErpOrderService;
import com.ziyun.erp.modules.order.service.OrderInfoService;
import com.ziyun.erp.modules.production.entity.ErpOrderItemEntity;
import com.ziyun.erp.modules.production.entity.ErpOrderProductionEntity;
import com.ziyun.erp.modules.production.operate.entity.AllotStateEntity;
import com.ziyun.erp.modules.production.operate.entity.AuditStateEntity;
import com.ziyun.erp.modules.production.operate.entity.LogisticsStateEntity;
import com.ziyun.erp.modules.production.service.ErpOrderItemService;
import com.ziyun.erp.modules.production.service.ErpOrderProductionService;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.SpringContextUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author guizhao
 * @Title: 待收货状态
 * @ProjectName ziyun-service-erp
 * @date 2018-09-08 16:00
 */
public class WaitReceiveState implements ErpOrderState {

    private static Logger logger = LoggerFactory.getLogger(WaitReceiveState.class);

    private ErpOrderService erpOrderService = SpringContextUtils.getBean("erpOrderService", ErpOrderService.class);
    private OrderInfoService orderInfoService = SpringContextUtils.getBean("orderInfoService", OrderInfoService.class);
    private ErpOrderProductionService erpOrderProductionService = SpringContextUtils.getBean("erpOrderProductionService", ErpOrderProductionService.class);
    private ErpOrderItemService erpOrderItemService = SpringContextUtils.getBean("erpOrderItemService", ErpOrderItemService.class);
    private ErpOrderLogService erpOrderLogService = SpringContextUtils.getBean("erpOrderLogService", ErpOrderLogService.class);

    @Override
    public R audit(AuditStateEntity auditStateEntity) {
        return R.error("不支持操作！");
    }

    @Override
    public R allot(AllotStateEntity allotStateEntity) {
        return R.error("不支持操作！");
    }

    @Override
    public R receive(Integer productionId) {
        logger.info("=================order receive start==================");
        logger.info("=================order receive params：productionId["+ productionId +"]");
        ErpOrderProductionEntity erpOrderProduction = erpOrderProductionService.selectById(productionId);
        Integer productionStatus = erpOrderProduction.getProductionStatus();
        String orderNo = erpOrderProduction.getOrderNo();
        String productionOrder = erpOrderProduction.getProductionOrder();
        String allotType = erpOrderProduction.getAllotType();
        if (productionStatus.intValue() != OrderProductionStatusEnum.FINISH.toInt()){
            return R.error("生产单状态异常！");
        }
        Map<String, Object> updateMap = new HashMap<>();
        if (StringUtils.equals(allotType, AllotTypeEnum.ORDER.toCode())){
            // 订单状态 生产中 =》 生产完成
            updateMap.put("productionStatus", ProductionStatusEnum.PRODUCTION_FINISH.toInt());
            updateMap.put("finishTime", DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            erpOrderService.updateByMap(updateMap, orderNo);

            // 商品状态 生产中 =》 生产完成
            List<ErpOrderItemEntity> erpOrderItemList = erpOrderItemService.selectListByErpOrder(orderNo);
            List<Integer> ids = erpOrderItemList.stream().map(ErpOrderItemEntity::getEoItemId).collect(Collectors.toList());
            updateMap.clear();
            updateMap.put("status", OrderItemStatusEnum.PRODUCTION_FINISH.toInt());
            erpOrderItemService.updateBatchByMap(updateMap, ids);
        }else if (StringUtils.equals(allotType, AllotTypeEnum.GOODS.toCode())){
            // 商品状态 生产中 =》 生产完成
            String erpItemId = erpOrderProduction.getErpItemId();
            updateMap.put("status", OrderItemStatusEnum.PRODUCTION_FINISH.toInt());
            erpOrderItemService.updateByMap(updateMap, Integer.valueOf(erpItemId));

            // 订单状态 生产中 =》 生产完成
            erpOrderProductionService.erpOrderItemFinish(orderNo);
        }else if (StringUtils.equals(allotType, AllotTypeEnum.PROCEDURE.toCode())){
            // 商品状态 生产中 =》 生产完成
            String erpItemId = erpOrderProduction.getErpItemId();
            erpOrderProductionService.productionFinish(Integer.valueOf(erpItemId));

            // 订单状态 生产中 =》 生产完成
            erpOrderProductionService.erpOrderItemFinish(orderNo);
        }

        // 修改分配订单号的收货状态
        updateMap.clear();
        updateMap.put("receivedStatus", ReceivedStatusEnum.RECEIVED.toInt());
        orderInfoService.updateByMap(updateMap, productionOrder);
        erpOrderService.updateByMap(updateMap, productionOrder);

        erpOrderLogService.insertOrderLog(orderNo, "生产单验货确定，productionId：" + productionId);

        logger.info("=================order receive end====================");
        return R.ok();
    }

    @Override
    public R addLogistics(LogisticsStateEntity logisticsStateEntity, String userId, String createName) {
        return R.error("不支持操作！");
    }

    @Override
    public R allotPriceFinish(Integer productionId) {
        return R.error("不支持操作！");
    }

    @Override
    public R cancleAllot(Integer productionId) {
        return R.error("不支持操作！");
    }

}
